import 'package:flutter/material.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:get/get.dart';
import 'package:mziot/app/config/constant.dart';
import 'package:mziot/app/config/design.dart';
import 'package:mziot/app/routes/app_pages.dart';
import 'package:mziot/app/tools/blue_service.dart';
import 'package:mziot/app/tools/device.dart';
import 'package:mziot/app/tools/global_const.dart';
import 'package:mziot/app/tools/public_provider.dart';
import 'package:mziot/generated/assets.dart';
import 'package:mziot/generated/locales.g.dart';

import '../controllers/auth_profile_controller.dart';

class AuthProfileView extends GetView<AuthProfileController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          LocaleKeys.mine_title.tr,
        ),
      ),
      backgroundColor: MColor.xFFEFF4FB,
      body: ListView(
        children: [
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          GestureDetector(
            child: Obx(() {
              return _subItem(Assets.imagesMeTime, LocaleKeys.mine_time_zone.tr,
                  controller.timeZone.value, true);
            }),
            onTap: () {
              Get.toNamed(Routes.TIME_ZONE, arguments: true)?.then((value) {
                controller.fetchMineInfo();
              });
            },
          ),
          Divider(height: 1, color: MColor.xFFCCCCCC),
          Obx(() {
            return _textItem(controller.time.value, TextAlign.right);
          }),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          GestureDetector(
            onTap: () {
              Get.toNamed(Routes.LANGUAGE, arguments: true)?.then((value) {
                controller.language.value =
                    GlobalConst.sharedPreferences?.getInt(Constant.kLanguage) ?? 999;
              });
            },
            child: Obx(() {
              var temp = "English";
              if (controller.language.value == 3) {
                temp = "Deutsch";
              } else if (controller.language.value == 2) {
                temp = "Allemagne";
              } else if (controller.language.value == 1) {
                temp = "中文";
              }
              return _subItem(Assets.imagesMeLanguage, LocaleKeys.mine_language.tr, temp, true);
            }),
          ),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          Obx(() {
            return _subItem(
                Assets.imagesMeAccount, LocaleKeys.mine_account.tr, controller.email.value, false);
          }),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          GestureDetector(
            child: _subItem(Assets.imagesMeHelp, LocaleKeys.mine_help.tr, null, true),
            onTap: () {
              Get.toNamed(Routes.HELP, arguments: true);
            },
          ),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          GestureDetector(
            child: _subItem(null, LocaleKeys.login_privacy.tr, null, true,
                icons: Icons.privacy_tip_outlined),
            onTap: () {
              Get.toNamed(
                Routes.INSWEB,
                parameters: {
                  'url': '${Server.web}protocol/privacy.html',
                  'title'
                      '': LocaleKeys.login_privacy.tr
                },
              );
            },
          ),
          Divider(height: 1, color: MColor.xFFCCCCCC),
          GestureDetector(
            child: _subItem(null, LocaleKeys.login_user.tr, null, true,
                icons: Icons.supervised_user_circle_outlined),
            onTap: () {
              Get.toNamed(
                Routes.INSWEB,
                parameters: {
                  'url': '${Server.web}protocol/agreement.html',
                  'title'
                      '': LocaleKeys.login_user.tr
                },
              );
            },
          ),
          Divider(height: 1, color: MColor.xFFCCCCCC),
          GestureDetector(
            child: _subItem(Assets.imagesMeAbout, LocaleKeys.mine_about.tr, null, true),
            onTap: () {
              Get.toNamed(Routes.INSWEB, parameters: {
                'title': LocaleKeys.mine_about.tr,
                'url': '${Server.web}protocol/aboutUs.html'
              });
            },
          ),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          _subItem(
            Assets.imagesMeVersion,
            LocaleKeys.mine_version.tr,
            LocaleKeys.mine_version_num.trParams({'version': DeviceUtils.version ?? '1.0.0'}),
            false,
          ),
          Divider(height: 12, color: MColor.xFFEFF4FB, thickness: 12),
          GestureDetector(
            child: _textItem(LocaleKeys.mine_out.tr, TextAlign.center),
            onTap: () async {
              BlueService.share.stopScan();
              BlueService.share.disConnectDevice();
              GlobalConst.tempModel = null;
              await GlobalConst.sharedPreferences?.remove(Constant.kUser);
              AppPages.INITIAL();
            },
          ),
        ],
      ),
    );
  }

  Widget _subItem(String? icon, String title, String? desc, bool more, {IconData? icons}) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 16),
      child: Row(
        children: [
          if (icon != null) ...{
            Image.asset(icon, width: 24, height: 24),
            SizedBox(width: 8),
          },
          if (icons != null) ...{
            Icon(
              icons,
              size: 24,
              color: MColor.skin,
            ),
            SizedBox(width: 8),
          },
          Text(
            title,
            style: MFont.regular13.apply(color: MColor.xFF333333),
          ),
          SizedBox(width: 50),
          if (desc != null) ...{
            Expanded(
              child: Text(
                desc,
                style: MFont.regular13.apply(color: MColor.xFF999999),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.right,
              ),
            ),
          } else ...{
            Spacer(),
          },
          SizedBox(width: 5),
          Offstage(
            offstage: !more,
            child: Icon(
              Icons.keyboard_arrow_right_outlined,
              size: 20,
              color: MColor.xFFCCCCCC,
            ),
          ),
        ],
      ),
    );
  }

  Widget _textItem(String text, TextAlign align) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 12, right: 12, top: 16, bottom: 16),
      child: Text(
        text,
        style: MFont.regular13.apply(color: MColor.xFF25C56F),
        textAlign: align,
      ),
    );
  }
}
